package com.sendinfo.sso;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sendinfo.sso.HttpUtil.SessionKey;

public class AuthHelperSSOImpl extends AuthHelperImpl implements AuthHelper {
	private enum SSOSite {
		HOME_SITE("localhost");

		private String domain;

		public String getDomain() {
			return this.domain;
		}

		private SSOSite(String domain) {
			this.domain = domain;
		}

		public static String[] domains() {
			SSOSite[] sites = SSOSite.values();
			String[] r = new String[sites.length];
			for (int i = 0; i < r.length; i++)
				r[i] = sites[i].getDomain();
			return r;
		}
	}

	@Override
	public Object checkUserId(HttpServletRequest req) {
		Object id = super.checkUserId(req);
		return id;
	}

	@Override
	public void putUserId(Object userId, HttpServletRequest req, HttpServletResponse resp) {
		super.putUserId(userId, req, resp);
		if(this.isSSO())
		HttpUtil.put(resp, new SessionKey[] { SessionKey.LOGINED_USER, SessionKey.LOGINED_DOMAIN }, new String[] {
				userId.toString(), this.getDomain() });
	}

	@Override
	public void removeUserId(HttpServletRequest req, HttpServletResponse resp) {
		super.removeUserId(req, resp);
		HttpUtil.remove(resp, SSOSite.domains(), new SessionKey[] { SessionKey.LOGINED_USER, SessionKey.LOGINED_DOMAIN });
	}
}
